/*
 * Created on 08.10.2006 by leck
 *
 */
package de.devisnik.eidle;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.widgets.Composite;

import de.devisnik.eidle.screen.IScreen;
import de.devisnik.eidle.screen.IScreenListener;

public class ScreenHandler implements IScreenHandler {

	private final Composite itsComposite;
	private IScreen itsCurrentScreen;
	private final List/* <IScreenListener> */itsListeners = new ArrayList/*
																		 * <IScreenListener
																		 * >
																		 */();

	public ScreenHandler(final Composite composite) {
		itsComposite = composite;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.devisnik.eidle.IScreenHandler#activate(de.devisnik.eidle.screen.IScreen
	 * )
	 */
	public void activate(final IScreen screen) {
		if (itsCurrentScreen != null) {
			removeListeners(itsCurrentScreen);
			disposeControl(itsCurrentScreen);
		}
		createControl(screen);
		itsCurrentScreen = screen;
		addListeners(itsCurrentScreen);
		itsCurrentScreen.setFocus();
		itsComposite.layout(true);
	}

	private void createControl(final IScreen screen) {
		log("calling createControl: " + screen.getClass().getName());
		screen.createControl(itsComposite);
	}

	private void disposeControl(final IScreen screen) {
		log("calling disposeControl: " + screen.getClass().getName());
		screen.disposeControl();
	}

	private void addListeners(final IScreen screen) {
		for (final Iterator iterator = itsListeners.iterator(); iterator
				.hasNext();) {
			final IScreenListener listener = (IScreenListener) iterator.next();
			screen.addScreenListener(listener);
		}
	}

	private void removeListeners(final IScreen screen) {
		for (final Iterator iterator = itsListeners.iterator(); iterator
				.hasNext();) {
			final IScreenListener listener = (IScreenListener) iterator.next();
			screen.removeScreenListener(listener);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.devisnik.eidle.IScreenHandler#dispose()
	 */
	public void dispose() {
		if (itsCurrentScreen != null) {
			removeListeners(itsCurrentScreen);
			disposeControl(itsCurrentScreen);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.devisnik.eidle.IScreenHandler#getCurrent()
	 */
	public IScreen getCurrent() {
		return itsCurrentScreen;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.devisnik.eidle.IScreenHandler#addScreenListener(de.devisnik.eidle.
	 * screen.IScreenListener)
	 */
	public void addScreenListener(final IScreenListener listener) {
		itsListeners.add(listener);
		if (itsCurrentScreen != null) {
			itsCurrentScreen.addScreenListener(listener);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.devisnik.eidle.IScreenHandler#removeScreenListener(de.devisnik.eidle
	 * .screen.IScreenListener)
	 */
	public void removeScreenListener(final IScreenListener listener) {
		itsListeners.remove(listener);
		if (itsCurrentScreen != null) {
			itsCurrentScreen.removeScreenListener(listener);
		}
	}

	private void log(String message) {
		EidleActivator activator = EidleActivator.getDefault();
		if (activator != null)
			activator.log(message);

	}
}
